#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int rk(const char *t, const char *p)
{
    int tlen, plen, *tvalue, pvalue;
    char *tmp;
    int i, j;
    int n10;
    
    if (t == NULL || p == NULL)
        return -1;
    
    tlen = strlen(t);
    plen = strlen(p);

    tvalue = malloc(sizeof(int) * (tlen-plen+1));
    tvalue[0] = pvalue = 0;
    for (i = 0; i < plen; i++) {
        tvalue[0] = tvalue[0]*10+t[i]-'0';
        pvalue = pvalue*10+p[i]-'0';
    }

    n10 = 1;
    for (i = 0; i < plen-1; i++)
        n10 *= 10;

    for (i = 0; i < tlen-plen+1; i++) {
        if (tvalue[i] % 13 == pvalue % 13) {
            for (j = 0; j < plen; j++) {
                if (t[i+j] != p[j])
                    break;
            }
            if (j == plen) {
                free(tvalue);
                return i;
            }
        }
        
        tvalue[i+1] = (tvalue[i]-(t[i]-'0')*n10)*10+t[i+plen]-'0';
    }

    free(tvalue);
    return -1;
}

int main(int argc, char const* argv[])
{
    char *s = "012345678901234567890123456789";
    char *p = "901";

    printf("s is %s\n", s);
    printf("find %s in s is %d\n", p, rk(s, p));

    p = "01235";
    printf("find %s in s is %d\n", p, rk(s, p));

    p = "012";
    printf("find %s in s is %d\n", p, rk(s, p));

    return 0;
}
